package com.nit.netty.bean;

import com.nit.netty.bean.invoker.NettyInterfaceInvoker;
import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.Proxy;

/**
 * @Author: whb
 * @Description:
 * @CreateTime: 2020/8/21 10:14
 */
public class NettyFactoryBean<T> implements FactoryBean<T> {
    private Class<T> nettyInterface;

    public NettyFactoryBean() {}

    public NettyFactoryBean(Class<T> nettyInterface) {
        this.nettyInterface = nettyInterface;
    }

    // 打了@NettyClient注解的类被Autowired的时候, 使用这里的getObj
    @Override
    public T getObject() throws Exception {
        // 返回代理, 代理类是我们的invoker
        return (T) Proxy.newProxyInstance(nettyInterface.getClassLoader(), new Class[]{nettyInterface}, NettyInterfaceInvoker.getInstance());
    }

    @Override
    public Class<?> getObjectType() {
        return this.nettyInterface;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
